package com.jlsoft.web.admin.qr;

import cn.hutool.core.codec.Base64;
import com.jlsoft.config.SimpleResponse;
import com.jlsoft.web.admin.qr.service.IQrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/qr/")
public class QrController {

    /**
     * 对传入的数据进行校验
     * 生成图片的base64编码文件
     * @param qrtype
     * @return
     */
    @GetMapping("/")
    public SimpleResponse qrIndxMethod(@RequestParam("qrtype") String qrtype) {
       //1： 对要转码的数据进行验证
        SimpleResponse validateResult = qrService.validateQrContent(qrtype);
        if(!validateResult.getFlag())
            return validateResult;

        //2: 生成图片的base64文件，并返回
        String imageBase64 = "data:image/png;base64,"+Base64.encode(qrService.createQrCode(qrtype));
        return new SimpleResponse(0, imageBase64, true);
    }

    @Autowired
    private IQrService qrService;

}
